<?
require_once("../include/class/XMLWriter.class.php");
include_once("../include/dbConnect.php");

class XMLFactory {
var $name; 				//for xml namespace
var $sql;  			//for fecth data from mysql database
var $type="detail";		//1. table  2. detail
var $variable=array();	//for save attribute will be writen

function initialize($name,$sql,$type,$variable){
	$this->name=$name;
	$this->sql=$sql;
	$this->type=$type;
	if(is_array($variable)){
		$this->variable=array_merge($this->variable,$variable);
	}
}

function tableProcess(){
	$this->xml_object=&new XMLWriter;
	$this->noattributes=array();
	$this->xml_object->addtag($this->name,$this->noattributes,'',$this->root,1);
	$query=mysql_query($this->sql);
	$numOfField=mysql_num_fields($query);
	$this->array_key=array_keys($this->variable);
	$this->array_value=array_values($this->variable);
	$num=count($this->array_key);
		
	$j=0;
	while($data=mysql_fetch_array($query)){
		
		$this->attributes=array();
		$this->attributes['number']=$j;
		$this->xml_object->addtag('row',$this->attributes,$this->root,$this->toptag,$j);
		for($i=0;$i<$num;$i++){
			for($k=0;$k<$numOfField;$k++){
				$name=mysql_field_name($query,$k);
				if(trim($this->array_key[$i])==trim($name)){
					$this->content=$data[$k];
					if($k==0)
						$this->content=base64_encode($data[$k].session_id());
					$this->xml_object->adddatatag($this->array_value[$i],$this->noattributes,$this->content,$this->toptag,$this->datatag);
				}
			}
		}
		$j++;
	}
	$this->xml_object->addtag('url',$this->noattributes,$this->root,$this->toptag,$j);
	$this->xml_object->adddata($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],$this->toptag,$this->path);
	
}

function detailProcess(){
	$this->xml_object=&new XMLWriter;
	$this->noattributes=array();
	$this->xml_object->addtag($this->name,$this->noattributes,'',$this->root,1);
	$query=mysql_query($this->sql);
	$numOfField=mysql_num_fields($query);
	$this->array_key=array_keys($this->variable);
	$this->array_value=array_values($this->variable);
	$num=count($this->array_key);
		
	$data=mysql_fetch_array($query);
	$j=1;
	for($i=0;$i<$num;$i++){
		for($k=0;$k<$numOfField;$k++){
			$name=mysql_field_name($query,$k);
			if(trim($this->array_key[$i])==trim($name)){
				$this->content=$data[$k];
				if($k==0)

					$this->content=base64_encode($data[$k].session_id());
				$this->xml_object->addtag($this->array_value[$i],$this->noattributes,$this->root,$this->toptag,$j);
				$this->xml_object->adddata($this->content,$this->toptag,$this->path);
		$j++;
			}
		}

	}
	
	$this->xml_object->addtag('url',$this->noattributes,$this->root,$this->toptag,$j);
	$this->xml_object->adddata($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],$this->toptag,$this->path);
	
}


function extractXML($output){
	if($this->type=="detail")
		$this->detailProcess();
	else
		$this->tableProcess();
	$this->xml_object->write($output);
	return $output;
}

function error(){
	return $this->xml_object->error;
}

}
?>